/**
 * 二叉搜索树
 * 左边的值都比根节点小，右边的值都比根节点大
 */
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root = null;

    public boolean search(int val) {
        TreeNode cur = root;
        while(cur != null){
            if(cur.val > val){
                cur = cur.left;
            } else if (cur.val <val) {
                cur = cur.right;
            }else {
                return true;
            }
        }
        return false;
    }


    //二叉搜索树不能插一个相同的数据
    public boolean insert(int val){
        TreeNode node = new TreeNode(val);
        //头节点为空直接把 val插入
        if(root == null) {
            root = node;
            return true;
        }


        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null){
            if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else {
                return false;
            }
        }
        //将val插入parent 的左边或右边
        if(parent.val > val){
            parent.left = node;
        }else {
            parent.right = node;
        }
        return true;
    }


    public void remove(int val){
        TreeNode cur = root;
        TreeNode parent = null;
        //找到要删除的节点
        while(cur != null){
            if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else {
                //删除的逻辑
                removeNode(parent,cur);
                return;
            }
        }

    }

    private void removeNode(TreeNode parent,TreeNode cur){
        /**
         * cur代表要删除的节点，parent是要删除节点的父亲节点
         *三种情况
         * 左边为空
         * 右边为空
         * 两边都不为空
         */
        if(cur.left == null){
            /**
             * 又分三种情况
             * 根节点
             * 是父亲节点的左边
             * 是父亲节点的右边
             */
            if(cur == root){
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
        }

        if(cur.right == null){
            /**
             * 又分三种情况
             * 根节点
             * 是父亲节点的左边
             * 是父亲节点的右边
             * */
            if(cur == root){
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }

        if(cur.right != null && cur.left!= null){
            //替罪羊 两种方式：找到要删除节点 的右边最左边的节点 右边最小的 或者 找到要删除节点 下面的左边最右边的节点 左边最大的
            //这里选取 右边最左边的节点(因为是最左边的节点，所以可能这个节点还有右子树，但是肯定没有左子树了)
            TreeNode targetParent = cur;
            TreeNode target = cur.right;

            while(target.left != null){
                targetParent = target;
                target = target.left;
            }

            cur.val = target.val;

            if(target == targetParent.left){
                targetParent.left = target.right;
            }else {
                targetParent.right = target .right;
            }


        }

    }


}
